﻿/*
   Created 2009 December 6, Randall Maas
   Copyright (c) 2009, Randall Maas
   All rights reserved.

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions are met:

   Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.

   Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

   Neither the name of Blackwood Designs, LLC nor the names of its contributors
   may be used to endorse or promote products derived from this software without
   specific prior written permission.

   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
   THE POSSIBILITY OF SUCH DAMAGE.
*/

using System;
using System . Runtime . InteropServices;
using System . Runtime . InteropServices.ComTypes;

enum KsProperty_LP1
{
   Version,
   DIGITAL_PAN,
   DIGITAL_TILT,
   DIGITAL_ZOOM,
   DIGITAL_PANTILTZOOM,
   Exposure,
   Face_Tracking,
   LED,
   FINDFACE,
}

[StructLayout(LayoutKind.Sequential)]
struct KSPROPERTY_LP1_HEADER
{
   uint ulFlags;
   uint ulReserved1;
   uint ulReserved2;
}

[StructLayout(LayoutKind.Sequential)]
struct KSPROPERTY_LP1_VERSION_S
{
   KSPROPERTY_LP1_HEADER	Header;
   ushort usMajor;
   ushort usMinor;
}

[StructLayout(LayoutKind.Sequential)]
struct KSPROPERTY_LP1_EXPOSURE_TIME_S
{
   KSPROPERTY_LP1_HEADER Header;
   public uint  ExposureTime;
}

[StructLayout(LayoutKind.Sequential)]
struct KSPROPERTY_LP1_FACE_TRACKING_S
{
   KSPROPERTY_LP1_HEADER	Header;
   public uint Mode;			// See LVUVC_LP1_FACE_TRACKING_MODE
}

enum PluralMode
{
   None,
   Single,
   Multiple
}

partial class VideoSource
{
   static Guid PROPSETID_LOGITECH_PUBLIC1 = new Guid("CAAE4966-272C-44a9-B792-71953F89DB2B");
   bool IsLogitech= false;

   /// <summary>
   /// Internally this is used to check to see if we have a Logitech APUI we can
   /// tweak
   /// </summary>
   void CheckForLogitech()
   {
      uint Ret;
      int Size = Marshal . SizeOf(typeof(KSPROPERTY_LP1_VERSION_S));
      IntPtr Ptr = Marshal . AllocCoTaskMem(Size);
      try
      {
         int Err = PropSet . Get(ref PROPSETID_LOGITECH_PUBLIC1, (uint) KsProperty_LP1 . Version,
                                 IntPtr.Zero, 0,
                                 Ptr, (uint) Size, out Ret);
         KSPROPERTY_LP1_VERSION_S version2 = (KSPROPERTY_LP1_VERSION_S)
             Marshal . PtrToStructure(Ptr, typeof(KSPROPERTY_LP1_VERSION_S));
         IsLogitech = 0 == Err;
      }
      finally
      {
         Marshal . FreeCoTaskMem(Ptr);
      }
   }

   public PluralMode FaceTracking
   {
      set
      {
         if (!IsLogitech)
           return ;
         KSPROPERTY_LP1_FACE_TRACKING_S FT = new KSPROPERTY_LP1_FACE_TRACKING_S();
         switch(value)
         {
            case PluralMode . None  : FT . Mode = 0; break;
            case PluralMode . Single: FT . Mode = 1; break;
            case PluralMode . Multiple: FT . Mode = 2; break;
         }
         int Size = Marshal . SizeOf(FT);
         IntPtr Ptr = Marshal . AllocCoTaskMem(Size);
         Marshal . StructureToPtr(FT, Ptr, true);
         try
         {
            int Err = PropSet . Set(ref PROPSETID_LOGITECH_PUBLIC1,
                                    (uint) KsProperty_LP1 . Face_Tracking,
                                     IntPtr.Zero, 0,
                                     Ptr, (uint) Size);
         }
         finally
         {
            Marshal . FreeCoTaskMem(Ptr);
         }
      }
   }


   /// <summary>
   /// Sets/gets the exposure time
   /// </summary>
   double LP1_ExposureTime
   {
      get
      {
         int Size = Marshal . SizeOf(typeof(KSPROPERTY_LP1_EXPOSURE_TIME_S));
         IntPtr Ptr = Marshal . AllocCoTaskMem(Size);
         try
         {
            uint Ret;
            int Err = PropSet . Get(ref PROPSETID_LOGITECH_PUBLIC1,
                                    (uint) KsProperty_LP1 . Exposure,
                                     IntPtr.Zero, 0,
                                     Ptr, (uint) Size, out Ret);
            KSPROPERTY_LP1_EXPOSURE_TIME_S ET = (KSPROPERTY_LP1_EXPOSURE_TIME_S)
               Marshal . PtrToStructure(Ptr, typeof(KSPROPERTY_LP1_EXPOSURE_TIME_S));
            return ET . ExposureTime / 10000.0;
         }
         finally
         {
            Marshal . FreeCoTaskMem(Ptr);
         }
      }
      set
      {
         KSPROPERTY_LP1_EXPOSURE_TIME_S E = new KSPROPERTY_LP1_EXPOSURE_TIME_S();
         E . ExposureTime = (uint)(10000*value+0.5);
         if (E . ExposureTime < 1) E . ExposureTime = 1;
         int Size = Marshal . SizeOf(E);
         IntPtr Ptr = Marshal . AllocCoTaskMem(Size);
         Marshal . StructureToPtr(E, Ptr, true);
         try
         {
            int Err = PropSet . Set(ref PROPSETID_LOGITECH_PUBLIC1,
                                    (uint) KsProperty_LP1 . Exposure,
                                     IntPtr.Zero, 0,
                                     Ptr, (uint) Size);
         }
         finally
         {
            Marshal . FreeCoTaskMem(Ptr);
         }
      }
   }

}
